Description

Learn the difference between Gateway and Client event scripts to understand when to use one vs the other.

Video recorded using: Ignition 8.1

Transcript

(open in window)

[00:00] In this lesson, I'll go over the differences between gateway and client event scripts. First of all, let's take a look at where you can find each of them from the designer. I'll look in the project browser for the scripting section. If I expand this, this is where I'll find the gateway events. Conversely, to find client event scripts, I'll find the Vision section and expand that, and they'll be found at client events. If I open client events, you'll see a list of the different client events that will trigger scripts to run. A quick example of this would be a client timer script. Adding a script to this event means that this script will fire based on some timer interval that I set, so if I set up a five second timer script, that script would run every five seconds. However, it's important to know that client event scripts are executing in the clients. That means that each client that opens will have its own timer and subsequent timer scripts that are running. If I were to launch 10 separate clients, that means that there are 10 separate scripts that execute for each client. That also means that if there are no clients running, then this script will not be firing.

[01:03] Another note about this is that this is specific to launching Vision projects. As a reminder, Perspective runtimes are referred to as sessions and session event scripts are covered in another lesson. I'll open up gateway event scripts next. Gateway events are similar in concept to client events except they're executed on the gateway. That means if I were to create a gateway timer script that results in one execution of one timer script, regardless of the number of clients, I'll bring back the client events window and you may have noticed that there's no keystroke and menu bar events available for the gateway events. That's because those events don't really make sense in the context of a gateway script. Speaking of context, it's important to keep in mind where these scripts are executing and be aware of scoping. If I add a print statement to a client event script, then the output of this will be sent to the client console. However, if I were to add a print statement to a gateway event script, then the output will be sent to the wrapper logs. The wrapper logs are files on the gateway that are found within the Ignition install directory. Accessing the gateway server may not be easy for everyone, so we'd recommend using one of our system functions system.util.getLogger.

[02:09] Instead, this function returns a logger object with whatever name I pass to it. And then I can use one of the available logging levels like info to log a message. When used from the gateway scope, this function will send messages to the diagnostics logs on the gateway webpage, and this is generally much easier to access for users. If a gateway script encounters an error, it will be thrown in the logs, but additionally, you can also visit the Gateway Scripts status page to see the current status and look at any of the error messages. You can find additional information about the getLogger function in the appendix of our user manual where we document every system function. These pages also reference the available scopes for each function, which will be useful when figuring out where certain scripts are called. For example, the system.gui.confirm function is a useful function for displaying a confirmation dialogue box to users. However, when looking at that user manual page, we'll see that it's only available in the Vision Client scope. If I were to try and use the function from a gateway event script, it won't display as an auto complete option and the script would error out.

[03:09] So be sure to keep scoping in mind when writing these event scripts. Finally, although gateway event scripts run in the gateway, it's important to remember that these are resources tied to a project. If I were to create a script and disable the project, the script would no longer fire, and if I were to create a backup of this project and export it to another gateway or duplicate it on the same gateway, that could result in the same script firing multiple times on the same trigger or timer and could open up the door to numerous problems. To summarize, gateway event scripts execute on the gateway and fire regardless of the number of clients. Where client event scripts execute in the client for every client that gets launched. Also, keep in mind the scope when writing these scripts. The scope of each function can be found in the appendix of our user manual, and I'll include a link to the system functions page below the video for your reference.

You are editing this transcript.

Make any corrections to improve this transcript. We'll review any changes before posting them.